import React from 'react';
import {connect} from 'react-redux';
import HeadNavComponent from "../../../components/headNav";
import Css from '../../../assets/css/user/mobile/index.css';
import {Switch,Toast} from "antd-mobile";
import request from "../../../assets/js/utils/request";
class mobpwdIndex extends React.Component{
    constructor(){
        super();
        this.state={
            bPassword:'',
            bStyle:'password'
        }
        this.sumbit=true
    }
    //输入密码
    changePassword(e){
        this.setState({bPassword:e.target.value})
    }
    //点击改变密码显示格式
    changeSwitch(){
        this.setState({checked:!this.state.checked},()=>{
            if (this.state.checked){
                this.setState({bStyle:'text'})
            }else {
                this.setState({bStyle:'password'})
            }
        })
    }
    //注册提交
    Submit(){
        if (this.state.bPassword.match(/^\s*$/)) {
            Toast.info('请输入密码',2);
            return false;
        }
        if (this.sumbit){
            this.sumbit=false;
            let sUrl=window.base.config.baseUrl+'user/myinfo/modpwd?token='+window.base.config.token,
                data={password:this.state.bPassword,uid:this.props.state.redLogin.uid};
            request(sUrl,'post',data).then((res)=>{
                if (res.code===200){
                    Toast.info(res.data,2,()=>{
                        this.props.history.goBack();
                    });
                }else {
                    Toast.info(res.data,2)
                }
            })
        }
    }
    //解决内存泄漏
    componentWillUnmount(){
        this.setState=(state,callback)=>{
            return ;
        }
    }
    render(){
        return(
            <div className={Css['main']}>
                <HeadNavComponent title={'修改密码'} ></HeadNavComponent>
                <div className={Css['grop']+' mt20'}>
                    <div className={Css['col_border']}>
                        <input className={Css['w42']} type={this.state.bStyle} placeholder='请输入密码' onChange={this.changePassword.bind(this)} value={this.state.bPassword}></input>
                        <div className={Css['col_switch']}>
                            <Switch checked={this.state.checked} onClick={this.changeSwitch.bind(this)}></Switch>
                        </div>
                    </div>
                </div>
                <div className={Css['grop']}>
                    <div className={Css['user_btn']} onClick={this.Submit.bind(this)}>修改</div>
                </div>
            </div>
        )
    }
}
export default connect((state)=>{
    return{
        state:state
    }
})(mobpwdIndex)
